package logic

import (
	"context"
	"errors"
	"house/models/mysql"

	"house/houserpc/houserpc"
	"house/houserpc/internal/svc"

	"github.com/zeromicro/go-zero/core/logx"
)

type NewPasswordLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewNewPasswordLogic(ctx context.Context, svcCtx *svc.ServiceContext) *NewPasswordLogic {
	return &NewPasswordLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *NewPasswordLogic) NewPassword(in *houserpc.NewPasswordRequest) (*houserpc.NewPasswordResponse, error) {
	// todo: add your logic here and delete this line

	var user mysql.User
	mysql.DB.Debug().Where("account", in.Account).Find(&user)

	if user.ID == 0 {
		return nil, errors.New("当前账号未注册")
	}
	users := mysql.User{
		Account:  in.Account,
		Password: in.Password,
	}
	mysql.DB.Debug().Updates(&users)

	return &houserpc.NewPasswordResponse{
		UserId: int64(user.ID),
	}, nil
}
